﻿using System;
using System.Windows.Data;
using System.Text;
using ReflectionStudio.Core;
using WPFExcelReport;
using WPFExcelReport.Helpers;
namespace ReflectionStudio.Controls
{
    /// <summary>
    /// Converter : Boolean to System.Windows.Visibility and not revert
    /// </summary>
    [ValueConversion(typeof(string), typeof(System.Windows.Visibility))]
    public class DateConverter : IValueConverter
    {
        /// <summary>
        /// IValueConverter.Convert
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return "";
            if (value.GetType().ToString().Contains("DBNull"))
                return "";
            string date = value.ToString();
            if (date.Trim().Length < 8)
                return date;
            date = date.Substring(0, 4) + "/" + date.Substring(4, 2) + "/" + date.Substring(6, 2);
            //0000012000
            return date;
        }

        /// <summary>
        /// IValueConverter.ConvertBack (not implemented)
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }
    [ValueConversion(typeof(string), typeof(System.Windows.Visibility))]
    public class NDateConverter : IValueConverter
    {
        /// <summary>
        /// IValueConverter.Convert
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return "";
            if (value.GetType().ToString().Contains("DBNull"))
                return "";
            string date = value.ToString();
            if (date.Trim().Length < 8)
                return date;
            date = date.Substring(0, 4) + "/" + int.Parse(date.Substring(4, 2)) + "/" + int.Parse(date.Substring(6, 2));
            //0000012000
            return date;
        }

        /// <summary>
        /// IValueConverter.ConvertBack (not implemented)
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            DateTime date = (DateTime)value;
            if (date == null)
                return value; 
            string year = date.Year.ToString();
            string month = Helper.Right("00" + date.Month.ToString(), 2);
            string day = Helper.Right("00" + date.Day.ToString(), 2); 
            return year + month + day;
        }
    }
    /// <summary>
    /// Converter : Boolean to System.Windows.Visibility and not revert
    /// </summary>
    [ValueConversion(typeof(string), typeof(System.Windows.Visibility))]
    public class DateNYR : IValueConverter
    {
        /// <summary>
        /// IValueConverter.Convert
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return "";
            if (value.GetType().ToString().Contains("DBNull"))
                return "";
            string date = value.ToString();
            if (date.Trim().Length < 8)
                return date;
            date = date.Substring(0, 4) + "年" + date.Substring(4, 2) + "月" + date.Substring(6, 2) + "日";
            //0000012000
            return date;
        }

        /// <summary>
        /// IValueConverter.ConvertBack (not implemented)
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }
    [ValueConversion(typeof(string), typeof(System.Windows.Visibility))]
    public class DateYR : IValueConverter
    {
        /// <summary>
        /// IValueConverter.Convert
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return "";
            if (value.GetType().ToString().Contains("DBNull"))
                return "";
            string date = value.ToString();
            if (date.Trim().Length < 8)
                return date;
            date = date.Substring(4, 2) + "月" + date.Substring(6, 2) + "日";
            //0000012000
            return date;
        }

        /// <summary>
        /// IValueConverter.ConvertBack (not implemented)
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }
}
